const root = {
  val: 'A',
  left: {
    val: 'B',
    left: {
      val: 'D'
    },
    right: {
      val: 'E'
    }
  },
  right: {
    val: 'C',
    left: {
      val: 'F'
    },
    right: {
      val: 'G'
    }
  }
}


function levelOrder(root) {
  if (!root) return [];

  const result = [];
  const queue = [root]; // 作为整棵树的根节点入队

  while (queue.length) {
    const current = queue.shift(); //看成父节点出队列(实际可能是一颗树)
    if (current.left) queue.push(current.left); //看成左边子节点入队 (实际左子树入队)
    if (current.right) queue.push(current.right); //看成右边子节点入队 (实际右子树入队)
    result.push(current.val); //记录出队的节点,经过上面的三行代码,一定是一层一层的出队,然后入队
  }

  return result;
}

const result = levelOrder(root)


result.forEach(item =>{
  process.stdout.write(item+" ")
})